package war
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	public class Cards extends Sprite
	{
		private var txt:TextField;
		private var a:Addpic=new Addpic();
		private var b:Deal=new Deal();
		private var chuArr:Array=[];
		public function Cards()
		
		{
			super();
			
			txt=new TextField();
			txt.text="出牌";	
			txt.backgroundColor=0xeff456;			
			txt.x=400;
			txt.y=0;
			addChild(a);
			addChild(txt);
			txt.addEventListener(MouseEvent.CLICK,chuClick);	
		}
		/* 出牌
		* @param e
		* 
		*/		
		protected function chuClick(e:MouseEvent):void
		{	
			//-------------------------------玩家出牌------------------------------------------
			for (var i:int = a.mcArr.length-1; i >=0; i--) 
			{
				
				if(a.mcArr[i].chu){	
					trace(a.mcArr[i])
					a.mcArr[i].x=250+i*20;
					a.mcArr[i].y=200;
					if(a.mcArr[i].parent){
						 a.removeChild(a.mcArr[i]);
						 chuArr.push(a.mcArr.splice(i,1));
						 chuArr.sortOn("value",Array.DESCENDING | Array.NUMERIC);
					}
					/*for (var j:int = 0; j < chuArr.length; j++) 
					{  
						addChild(chuArr[j]);
						chuArr[j].x=250+j*20;
						chuArr[j].y=200;
					}*/
				}			
			}			
			//----------------------------------------计时器--------------------------------------------
			var timer:Timer=new Timer(100,3);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,onTimerComlete);
			timer.start();			
			
		}
		/**
		 * 电脑出牌
		 * @param event
		 * 
		 */		
		protected function onTimerComlete(event:TimerEvent):void
		{			
			for (var i:int = a.mcPlayer1Arr.length-1; i >=0; i--) 
			{				
				for (var j:int = 0; j < chuArr.length; j++) 
				{
					if (b.arr2[i].value>chuArr[j].value) 
					{						
						//mcPlayer1Arr[i].x=i*20+200;
						//mcPlayer1Arr[i].y=200;
						//-------------------------清空玩家中转数组--------------------------------
						if(chuArr.length!=0){
							for (var k:int = chuArr.length-1; k >=0 ; k--) {  
								removeChild(chuArr[k]);
								chuArr.splice(k,1)[0];
							}
						}
						removeChild(a.mcPlayer1Arr[i]);
						chuArr.push(a.mcPlayer1Arr.splice(i,1)[0]);
						for (var k1:int = 0; k1 < chuArr.length; k1++) 
						{  
							addChild(chuArr[k1]);
							chuArr[k1].x=250+k1*20;
							chuArr[k1].y=200;
						}
						return;
					}
				}								
			}
			
			
		}
	}
}